package com.runfast.paotui.web.controller.server;

import com.runfast.common.utils.SessionContext;
import com.runfast.common.web.entity.Result;
import com.runfast.paotui.dao.model.Order;
import com.runfast.paotui.dao.model.OrderStatusHistory;
import com.runfast.paotui.dao.model.OrderStatusHistoryExample;
import com.runfast.paotui.service.OrderService;
import com.runfast.paotui.service.OrderStatusHistoryService;
import com.runfast.waimai.dao.model.*;
import com.runfast.waimai.service.WaiMaiRunfastAgentbusinessService;
import com.runfast.waimai.service.WaiMaiRunfastCuseraddressService;
import com.runfast.waimai.service.WaiMaiRunfastShopperService;
import com.runfast.waimai.utils.TimeUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController("serverOrderController")
@RequestMapping("/server/paotui/order")
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private WaiMaiRunfastAgentbusinessService waiMaiRunfastAgentbusinessService;
    @Autowired
    private WaiMaiRunfastCuseraddressService waiMaiRunfastCuseraddressService;
    @Autowired
    private OrderStatusHistoryService orderStatusHistoryService;
    @Autowired
    private WaiMaiRunfastShopperService waiMaiRunfastShopperService;
    /**跑腿订单查询带分页*/
    @GetMapping("/list")
    public Result list(@PageableDefault Pageable pageable, Order order ) throws ParseException {
        Map<Object,Object> map=new HashMap<Object, Object>();
        Map<Object,Object> map1=new HashMap<Object, Object>();
        BaseUser admin= SessionContext.getAdmin();
        WaiMaiRunfastAgentaccounts agentaccounts= SessionContext.getCurrentRunfastAgentaccounts();
        if(admin!=null && admin.getType()==2 && agentaccounts!=null){
            WaiMaiRunfastAgentbusiness agentbusiness=waiMaiRunfastAgentbusinessService.selectByPrimaryKey(agentaccounts.getAgentid());
            if(agentbusiness!=null){
                map1.put("agentIds",agentbusiness.getId());
            }else{return Result.fail(null,"找不到对应的代理商！");}
        }else if(admin!= null &&  admin.getType()==1){
            //后台用户
            if(order!=null &&StringUtils.isNotBlank(order.getAgentIds())){
                map1.put("agentIds",order.getAgentIds());
            }
        }
        if(order!=null){
            /** 订单号*/
            if(StringUtils.isNotBlank(order.getOrderNo()) ){
                map1.put("orderNo",order.getOrderNo());
            }
            /** 支付方式*/
            if(order.getPaymethod()!=null ){
                map1.put("paymethod",order.getPaymethod());
            }
            /**查询时间*/
            if(StringUtils.isNotBlank(order.getDate())){
                List<String> result = Arrays.asList(order.getDate().split(","));
                map1.put("startTime",TimeUtil.getDate(result.get(0)));
                map1.put("endTime",TimeUtil.getDate(result.get(1)));
            }
        }
        map1.put("pageable",pageable);
        List<Order> list =orderService.selectByMapWithPageable(map1);
        if(list.size()>0){
             for(Order order1:list){
                   if(order1.getToId()!=null){
                       WaiMaiRunfastCuseraddress cuseraddress=waiMaiRunfastCuseraddressService.selectByPrimaryKey(order1.getToId());
                       if(cuseraddress!=null){
                           order1.setCuseraddress(cuseraddress);
                       }
                   }
             }
        }
        Integer total=orderService.countByMap(map1);
        map.put("list",list);
        map.put("total",total);
        return Result.ok(null,map);
    }

    /*** 订单信息*/
    @GetMapping("/info")
    public Result info(Integer id){
        Order order =new Order();
        if(id!=null){
            order =orderService.selectByPrimaryKey(id);
            if(order !=null){
                //订单状态改变表--订单进度
                OrderStatusHistoryExample example1=new OrderStatusHistoryExample();
                example1.createCriteria().andOrderIdEqualTo(order.getId());
                List<OrderStatusHistory> status=orderStatusHistoryService.selectByExample(example1);
                order.setOrderStatusHistoryList(status);
                //客户地址信息
                if(order.getToId()!=null){
                    WaiMaiRunfastCuseraddress cuseraddress=waiMaiRunfastCuseraddressService.selectByPrimaryKey(order.getToId());
                    if(cuseraddress!=null){
                        order.setCuseraddress(cuseraddress);
                    }
                }
                //骑手信息
                if(order.getDriverId()!=null){
                    WaiMaiRunfastShopper waiMaiRunfastShopper=waiMaiRunfastShopperService.selectByPrimaryKey(order.getDriverId());
                    if(waiMaiRunfastShopper!=null){
                        order.setWaiMaiRunfastShopper(waiMaiRunfastShopper);
                    }
                }
            }else{return Result.fail(null,"找不对对应的订单！");}
        }else{return Result.fail(null,"传入参数不对！");}
        return Result.ok(null,order);
    }




}

